package com.player.rest.resource;

import java.util.logging.Logger;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import com.player.rest.exception.FaultInfo;
import com.player.rest.exception.ServiceErrorCode;
import com.player.rest.util.ResponseConverter;
import com.player.rest.util.StackTraceUtil;

@Provider
public class RuntimeExceptionMapper implements ExceptionMapper<RuntimeException> {

	private static final Logger log = Logger.getLogger(RuntimeExceptionMapper.class.getName());

	public Response toResponse(RuntimeException ex) {
		log.severe("Internal error: " + StackTraceUtil.toString(ex));
		FaultInfo faultInfo = new FaultInfo(ServiceErrorCode.INTERNAL_ERROR, ex.getMessage(), null, ex.getClass()
				.getSimpleName());
		return Response.status(Status.INTERNAL_SERVER_ERROR).entity(ResponseConverter.toJSON(faultInfo)).build();
	}

}
